package com.ciwong.epaper.modules.epaper.adapter;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.epaper.bean.VideoRecord;
import com.ciwong.epaper.modules.msg.util.MsgDateFormat;
import com.ciwong.epaper.util.DisplayUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

import java.util.List;

/**
 * Created by byou
 */

public class DubVideoRecordListAdapter extends BaseQuickAdapter<VideoRecord, DubVideoRecordListAdapter.BookDeskViewHolder> {


    public DubVideoRecordListAdapter(int layoutResId, @Nullable List<VideoRecord> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BookDeskViewHolder helper, VideoRecord item) {
        helper.bindData(item);
    }


    public static class BookDeskViewHolder extends BaseViewHolder {
        private Uri uri;
        private VideoRecord mVideoRecord;
        private SimpleDraweeView mIvIcon;
        private TextView mTvName, mTcScore, mTvTime;

        public BookDeskViewHolder(View view) {
            super(view);
        }

        public void bindData(VideoRecord videoRecord) {
            mVideoRecord = videoRecord;
            mIvIcon = getView(R.id.iv_icon);
            mTvName = getView(R.id.tv_name);
            mTcScore = getView(R.id.tv_score);
            mTvTime = getView(R.id.tv_time);
            if (videoRecord != null) {
                if (!TextUtils.isEmpty(videoRecord.getCover())) {
                    uri = Uri.parse(videoRecord.getCover());
                    mIvIcon.setAspectRatio(1.76f);
                    setNetImg(mIvIcon, uri, mIvIcon.getWidth(), mIvIcon.getHeight());
                } else {
                    setLocalDefaultImg(mIvIcon);
                }
                mTvName.setText(videoRecord.getResourceName());
                mTcScore.setText(videoRecord.getActualScore() + "分");
                mTvTime.setText(MsgDateFormat.formatDateXieGan(videoRecord.getSubmitDate()));
            }
        }


        private void setNetImg(SimpleDraweeView simpleDraweeView, Uri uri, int width, int height) {
            try {
                ImageRequest request = null;
                if (uri != null) {
                    request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(DisplayUtils.dip2px(simpleDraweeView.getContext(), 100), DisplayUtils.dip2px(simpleDraweeView.getContext(), 200))).build();

                    DraweeController drawweeController = Fresco.newDraweeControllerBuilder().setUri(uri).setImageRequest(request)
                            //列表滑动会变流畅
                            .setOldController(simpleDraweeView.getController())
                            //                    .setAutoPlayAnimations(true)
                            .build();
                    simpleDraweeView.setController(drawweeController);
                }
            } catch (Exception e) {

            } catch (OutOfMemoryError outOfMemoryError) {

            }


        }

        private void setLocalDefaultImg(SimpleDraweeView simpleDraweeView) {
            Uri uri = Uri.parse("res:///" + R.mipmap.item_dub_video_default);
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setUri(uri)
                    .setTapToRetryEnabled(true)
                    .build();
            simpleDraweeView.setController(controller);
        }
    }
}
